<template>
  <div class="app-container">
    <manage-table
      :table-name="tableName"
      :service="service"
      :default-model="defaultModel"
      :table-options="options"
      ref="manageTable">
      <template #tableColumnActionOnReadOnlySuffix="{ row, actions }">
        <Authority :visible="actions.update">
          <el-button type="text" :icon="icons.Odometer" style="color: #e6a23c" @click="setAuthority(row)">
            设置权限
          </el-button>
        </Authority>
      </template>
      <template #tableColumnActionOnNormalSuffix="{ row, actions }">
        <Authority :visible="actions.update">
          <el-button type="text" :icon="icons.Odometer" style="color: #e6a23c" @click="setAuthority(row)">
            设置权限
          </el-button>
        </Authority>
      </template>
    </manage-table>
    <AuthorityDialog :current="current" :set-visible="setVisible" :visible="visible" />
  </div>
</template>

<script lang="ts">
  import ManageTable from '@/components/Table/ManageTable/index.vue';
  import { Options } from '@/components/Table/types';
  import { Resource, resourceService } from '@/services/authorization/Resource';
  import { defineComponent, ref } from 'vue';
  import AuthorityDialog from '@/views/system/Resource/AuthorityDialog.vue';
  import { useVisible } from '@/hooks/useVisible';
  import { TableRow } from '@/components/Table/ManageTable/model';
  import { RowStatus } from '@/components/Table/hooks/useTable';

  import { Odometer } from '@element-plus/icons-vue';

  export default defineComponent({
    name: 'ResourceTable',
    components: {
      AuthorityDialog,
      ManageTable,
    },
    setup() {
      const tableName = 'resource';
      const defaultModel: Partial<Resource> = {
        id: undefined,
        remark: undefined,
        type: undefined,
        content: undefined,
        method: undefined,
      };
      const options: Options<Resource> = {
        type: {
          type: 'binary-category',
          label: '资源类型',
          mappings: [
            {
              label: '后端接口',
              value: 0,
            },
            {
              label: '前端菜单',
              value: 1,
            },
          ],
        },
        content: {
          type: 'value',
          label: '内容',
        },
        method: {
          type: 'category',
          label: '获取方法',
          readOnly: (model: Resource) =>
            ((model as unknown as RowStatus).rowStatus === 1 || (model as unknown as RowStatus).rowStatus === 2) &&
            model.type === 1,
          mappings: [
            {
              label: 'All',
              value: 0,
            },
            {
              label: 'GET',
              value: 1,
            },
            {
              label: 'POST',
              value: 2,
            },
            {
              label: 'PUT',
              value: 3,
            },
            {
              label: 'PATCH',
              value: 4,
            },
            {
              label: 'DELETE',
              value: 5,
            },
          ],
        },
        remark: {
          type: 'value',
          label: '备注',
        },
      };
      const { setVisible, visible } = useVisible();
      const current = ref(null as TableRow | null);
      const setAuthority = (row: TableRow) => {
        current.value = row;
        setVisible();
      };
      const clearCurrent = () => {
        current.value = null;
      };
      return {
        tableName,
        service: resourceService,
        defaultModel,
        options,
        visible,
        setVisible: () => {
          setVisible();
          clearCurrent();
        },
        current,
        setAuthority,
        icons: {
          Odometer,
        },
      };
    },
  });
</script>
